var express = require('express');
var router = express.Router();

//加载模型
const CateModel = require('../../models')['Cate']
const NewsModel = require('../../models')['New']


router.get('/index', async function (req, res, next) {
  try {
    //查询新闻分类数据,按照排序数字的倒序排列
    const cates = await CateModel.findAll({
      order: [
        ['sort', 'DESC']
      ]
    });
    //图片新闻:访问量前5的数据
    const image_news = await NewsModel.findAll({
      attributes: ['id', 'title', 'img_url'],
      order: [
        ['view_nums', 'DESC']
      ],
      limit: 5
    })

    //最新新闻：按照时间倒序排列
    const new_news = await NewsModel.findAll({
      attributes: ['id', 'title', 'createdAt'],
      order: [
        ['createdAt', 'DESC']
      ],
      limit: 10
    })
    //最热新闻： 访问量倒序排列
    const hot_news = await NewsModel.findAll({
      attributes: ['id', 'title', 'createdAt', 'view_nums'],
      order: [
        ['view_nums', 'DESC']
      ],
      limit: 10
    })

    //返回数据
    res.cc(200, '请求成功', {
      cates,
      image_news,
      new_news,
      hot_news
    })

  } catch (e) {
    res.cc(400, e.message)
  }

});

module.exports = router;